//
//  ZYEmoticonTools.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/21.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

// 读取表情数据
class ZYEmoticonTools: NSObject {

    
    // 设置单例
    static let sharedEmoticonTools:ZYEmoticonTools = ZYEmoticonTools()
    
    // 构造函数私有化
    private override init() {
        
        super.init()
        
    }
    
    // 获取默认默认表情
    private lazy var defaultEmoticons:[ZYEmoticon] = {
        return self.loadEmoticonWithPath("default/info.plist")
    }()
    
    // 获取emoji表情
    private lazy var emojiEmoticons:[ZYEmoticon] = {
        
        return self.loadEmoticonWithPath("emoji/info.plist")
        
    }()
    
    // 获取lxh表情
    private lazy var lxhEmoticons:[ZYEmoticon] = {
        
        return self.loadEmoticonWithPath("lxh/info.plist")
    }()
    
    
    // 通过表情字符串 查找表情模型
    func emoticonWithEmoticonStr(emoticonStr: String) -> ZYEmoticon? {
        
        for value in defaultEmoticons {
            if value.chs == emoticonStr {
                return value
            }
        }
        
        for value in lxhEmoticons {
            if value.chs == emoticonStr {
                return value
            }
            
        }
        
        return nil
        
    }
    
    
    
    
    // 创建emoticonBundle对象
    private lazy var emoticonBundle:NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        let bundle = NSBundle(path: path)!
        return bundle
        
    }()
    
    // 获取全部表情数据
     lazy var allEmoticons:[[[ZYEmoticon]]] = {
        
       return [
         self.sectionWithEmoticon(self.defaultEmoticons),
         self.sectionWithEmoticon(self.emojiEmoticons),
         self.sectionWithEmoticon(self.lxhEmoticons)

        ]
        
    }()
    
    
    // 获取表情数组的方法
    private func loadEmoticonWithPath(subStr:String) -> [ZYEmoticon] {
        
        let path = self.emoticonBundle.pathForResource(subStr, ofType: nil)!
        
        // 加载plist数据源
        let array = NSArray(contentsOfFile: path)!
        var tempArray = [ZYEmoticon]()
        for dic in array {
            
            let emoticon = ZYEmoticon(dict: dic as! [String:AnyObject])
            // 判断是否是图片
            if emoticon.type == "0" {
                // path拼接路径
                // 获取图片名字
                let png = emoticon.png!
                // 去掉路径的最后部分
                let subPath = (path as NSString).stringByDeletingLastPathComponent
                // 拼接图片的全路径
                emoticon.path = subPath + "/" + png
  
            }
     
            tempArray.append(emoticon)
            
        }
        
        return tempArray
    }
    
    // 计算页数截取数据
    private func sectionWithEmoticon(emoticons:[ZYEmoticon]) -> [[ZYEmoticon]] {
        
        let pageCount = (emoticons.count - 1) / 20+1
        
        var tempArray:[[ZYEmoticon]] = [[ZYEmoticon]]()
        
        // 遍历页数截取数组
        
        for i in 0..<pageCount {
            // 计算当前索引
            let location = i * 20
            var length = 20
            
            if location + length > emoticons.count {
                
                length = emoticons.count - location
                
            }
            // 截取数组元素
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(location, length)) as! [ZYEmoticon]
            
            tempArray.append(subArray)
            
        }
        
        return tempArray
   
    }
    
    
    
    
}
